package ge.telasi.tasks.ui.flowpermission;

import ge.telasi.tasks.model.FlowPermission;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.SimpleTable;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.LoggerUtils;
import ge.telasi.tasks.ui.task.TaskDecontationUtils;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

/**
 * @author dimitri
 */
public class FlowPermissionTable extends SimpleTable<FlowPermission> {
    private static final long serialVersionUID = -2005194106030000289L;
    static final int OPERATION_IMG = 0;
    static final int OPERATION = 1;
    static final int ROLE = 2;
    static final int IMG_TASK_STATUS = 3;
    static final int TASK_STATUS = 4;
    static final int IMG_FINAL_STATUS = 5;
    static final int FINAL_STATUS = 6;
    
    private Integer taskType;

    public FlowPermissionTable() {
        super();
        setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnModel colModel = getColumnModel();
        colModel.getColumn(OPERATION_IMG).setResizable(false);
        colModel.getColumn(IMG_TASK_STATUS).setResizable(false);
        colModel.getColumn(IMG_FINAL_STATUS).setResizable(false);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    FlowPermission permission = getSelection();
                    if (permission != null) {
                        FlowPermissionPropertiesDialog dialog = new FlowPermissionPropertiesDialog(FlowPermissionTable.this);
                        dialog.setFlowPermission(permission);
                        UIUtils.openDialog(dialog);
                    }
                }
            }
        });
    }

    public int getTaskType() {
        return taskType;
    }

    public void setTaskType(int taskType) {
        this.taskType = taskType;
    }
    
    public void add() {
        if (taskType != null && Platform.getDefault().validateConnection()) {
            FlowPermissionDialog dialog = new FlowPermissionDialog(this);
            dialog.setTaskType(taskType);
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                add(dialog.getFlowPermission());
            }
        }
    }

    public void remove() {
        if (Platform.getDefault().validateConnection()) {
            List<FlowPermission> permissions = getSelections();
            Platform pl = Platform.getDefault();
            String msg = pl.getLocaleManager().getMessage("General.deleteByCountMsg", permissions.size());
            if (UIUtils.askConfirmation(this, msg)) {
                try {
                    pl.getFacade().deleteFlowPermissions(pl.getCredentials(), permissions);
                    for (FlowPermission permission : permissions) {
                        remove(permission);
                    }
                } catch (Exception ex) {
                    LoggerUtils.manageException(this, ex);
                }
            }
        }
    }

    @Override
    public JComponent[] allFields() {
        return new JComponent[] {};
    }

    @Override
    protected TableModel<FlowPermission> createEmptyModel() {
        return new FlowPermissionTableModel();
    }

    @Override
    protected List<FlowPermission> getDataFromServer(Platform pl) throws Exception {
        return pl.getFacade().getFlowPermissions(pl.getCredentials(), taskType);
    }

    @Override
    protected String getPreferenceName() {
        return "85938083011938930";
    }

    @Override
    protected int[] getDefaultColumnWidthes() {
        return new int[] { 20, 150, 100, 20, 200, 20, 200 };
    }

    class FlowPermissionTableModel extends TableModel<FlowPermission> {

        @Override
        public int getColumnCount() {
            return 7;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case OPERATION_IMG:
                case IMG_FINAL_STATUS:
                case IMG_TASK_STATUS:
                    return "";
                case OPERATION:
                    return "მოქმედება";
                case ROLE:
                    return "როლი";
                case TASK_STATUS:
                    return "დავალების სტატუსი";
                case FINAL_STATUS:
                    return "საბოლოო სტატუსი";
                default:
                    return "?";
            }
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case OPERATION_IMG:
                case IMG_FINAL_STATUS:
                case IMG_TASK_STATUS:
                    return Icon.class;
                case OPERATION:
                case ROLE:
                case TASK_STATUS:
                case FINAL_STATUS:
                    return String.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int column) {
            FlowPermission permission = getObjectAt(row);
            switch (column) {
                case OPERATION_IMG:
                    return FlowPermissionDecorationUtils.getOperationIcon(permission.getOperation());
                case IMG_FINAL_STATUS:
                    if (permission.getFinalStatus() != null) {
                        return TaskDecontationUtils.getStatusIcon(permission.getFinalStatus());
                    } else {
                        return null;
                    }
                case IMG_TASK_STATUS:
                    return TaskDecontationUtils.getStatusIcon(permission.getTaskStatus());
                case OPERATION:
                    return FlowPermissionDecorationUtils.getOperationName(permission.getOperation());
                case ROLE:
                    return TaskDecontationUtils.getRoleLocalizedName(permission.getRole());
                case TASK_STATUS:
                    return TaskDecontationUtils.getStatusName(permission.getTaskStatus());
                case FINAL_STATUS:
                    return permission.getFinalStatus() == null ? "" :
                        TaskDecontationUtils.getStatusName(permission.getFinalStatus());
                default:
                    return "?";
            }
        }

    }

}
